package jdos.cpu;

public class Table_ea extends Core {
    //typedef PhysPt (*EA_LookupHandler)(void);
    static public interface GetEAHandler {
        public /*PhysPt*/int call();
    }

    static final /*Bit32u*/long SIBZero=0;
    //INSTRUCTION PREFIX O~l
    //ADDRESS-SIZE PREFIX O~l
    //OPERAND-SIZE PREFIX O~l
    //SEGMENT OVERRIDE O~l
    //OPCODE 1 OR 2
    //MODR/M 0 OR l
    //SIB 0 OR l
    //DISPLACEMENT 0,1,2 OR 4
    //IMMEDIATE 0,1,2 OR 4
//    The following are the allowable instruction prefix codes:
//    	OF3H REP prefix (used only with string instructions)
//    	OF3H REPE/REPZ prefix (used only with string instructions)
//    	OF2H REPNE/REPNZ prefix (used only with string instructions)
//    	OFOH LOCK prefix

//    	The following are the segment override prefixes:
//    	2EH CS segment override prefix
//    	36H SS segment override prefix
//    	3EH DS segment override prefix
//    	26H ES segment override prefix
//    	64H FS segment override prefix
//    	65H GS segment override prefix
//    	66H Operand-size override
//    	67H Address-size override
//    	
    static public int getEaa16(int rm) {
    	boolean ss = false;
    	int val = 0;
        switch (rm & 7) {
	        case 0x00: ss = false; val = reg_ebx.word()+reg_esi.word();break;
	        case 0x01: ss = false; val = reg_ebx.word()+reg_edi.word();break;
	        case 0x02: ss =  true; val = reg_ebp.word()+reg_esi.word();break;
	        case 0x03: ss =  true; val = reg_ebp.word()+reg_edi.word();break;
	        case 0x04: ss = false; val = reg_esi.word();break;
	        case 0x05: ss = false; val = reg_edi.word();break;
	        case 0x06: ss =  true; val = reg_ebp.word();break;
	        case 0x07: ss = false; val = reg_ebx.word();break;
	    }
		int val2;
		if ((rm & 0xC0) == 0) {
			if ((rm & 7) == 0x06) {
				ss = false;
				val2 = Fetchw();
			} else {
				val2 = val;
			}
		} else if ((rm & 0x80) == 0) {
			val2 = val + Fetchbs();
		} else {
			val2 = val + Fetchws();
		}
		val2 = val2 & 0xFFFF;
		val2 += ss ? base_ss : base_ds;
		return val2;
    }

	static public int getEaa(int rm) {
			return getEaa16(rm);
	}

	protected static int GetEADirect() {
			return getEaa16(0x06);
	}
}
